package net.maku.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.bean.query.WebUserQuery;
import net.maku.bean.vo.WebUserVO;
import net.maku.convert.WebUserConvert;
import net.maku.entity.WebUserEntity;
import net.maku.mapper.WebUserMapper;
import net.maku.service.WebUserService;
import net.maku.utils.PageResult;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户信息表
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2024-09-02
 */
@Service
@AllArgsConstructor
public class WebUserServiceImpl extends BaseServiceImpl<WebUserMapper, WebUserEntity> implements WebUserService {

    @Override
    public PageResult<WebUserVO> page(WebUserQuery query) {
        IPage<WebUserEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(WebUserConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<WebUserEntity> getWrapper(WebUserQuery query){
        LambdaQueryWrapper<WebUserEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(WebUserVO vo) {
        WebUserEntity entity = WebUserConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(WebUserVO vo) {
        WebUserEntity entity = WebUserConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}